public class num160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//        ListNode prev1 = headA;
//        ListNode prev2 = headB;
//        while (prev1 != prev2){
//          prev1 = prev1 == null ? headB : prev1.next;
//          prev2 = prev2 == null ? headA : prev2.next;
//        }
//        return prev1;
        ListNode prev1 = headA;
        ListNode prev2 = headB;
        while (prev1 != prev2){
            if(prev1 == null){
                prev1 = headB;
            }else{
                prev1 = prev1.next;
            }
            if(prev2 == null){
                prev2 = headA;
            }else{
                prev2 = prev2.next;
            }
        }
        return prev1;
    }
}
